/*****************************************************************************
*
*  PROJECT:     SlipStream
*               (Texture Classes)
*  LICENSE:     GNU General Public License v3 - http://www.gnu.org/licenses/gpl.html

*  DEVELOPERS:  Philip Farquharson 	<B00137077@studentmail.uws.ac.uk>
*				John Gray 			<B00145524@studentmail.uws.ac.uk>		
*				Zohaib Bhatti		<B00104280@studentmail.uws.ac.uk>
*				Euan Hislop 		<B00138636@studentmail.uws.ac.uk>
*
*****************************************************************************/
#include "CTextureManager.h"
#include "lib.h"

template<> CTextureManager * CSingleton< CTextureManager >::m_pSingleton = NULL;

CTextureManager::CTextureManager ( void )
{

}

CTextureManager::~CTextureManager ( void )
{
	// clean up
	std::vector<CTexture*>::const_iterator iter = m_textures.begin ();
	for ( ; iter != m_textures.end(); ++iter )
	{
		CTexture* pTexture = static_cast < CTexture* > ( *iter );

		assert ( pTexture );

		// free our resources
		SAFE_DELETE( pTexture );
	}
}

CTexture* CTextureManager::FindTexture( const char *filename)
{
	std::vector<CTexture*>::const_iterator iter = m_textures.begin ();
	for ( ; iter != m_textures.end(); ++iter )
	{
		CTexture* pTexture = static_cast < CTexture* > ( *iter );

		assert ( pTexture );

		// free our resources
		if ( STRING_COMPARE(pTexture->getFilename(), filename) )
			return pTexture;
	}
	return NULL;
}

CTexture* CTextureManager::LoadTexture( const char *filename )
{
	// check we don't already have the same texture loaded
	std::vector<CTexture*>::const_iterator iter = m_textures.begin ();
	for ( ; iter != m_textures.end(); ++iter )
	{
		CTexture* pTexture = static_cast < CTexture* > ( *iter );

		assert ( pTexture );

		// free our resources
		if ( STRING_COMPARE(pTexture->getFilename(), filename) )
			return pTexture;
	}

	CTexture* pTexture = new CTexture ( filename );
	m_textures.push_back ( pTexture );
	return pTexture;
}

CTexture* CTextureManager::GetTexture ( int ID )
{
	return m_textures.at( ID );
}